package com.hisense.hiioc.hismartplate.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther:biexiande@hisense.com
 * @date:2019/3/5 10:04
 * @des 发送消息组件
 */
@Component
public class RabbitSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final String LINE_SPLITER ="\n";
    private static final Logger logger= LoggerFactory.getLogger(RabbitSender.class);

    //发送DeviceLog消息
    public void sendLog(String logContent) throws Exception {
        String [] logItems =logContent.split(LINE_SPLITER);
        ObjectMapper mapper = new ObjectMapper();
        for (String logItem :logItems){
            Map<String,String> logInfoMap = null;
            try {
                logInfoMap = mapper.readValue(logItem, HashMap.class);
            } catch (IOException e) {
                logger.error("日志文件序列化异常");
            }
            String logType = logInfoMap.get("type");
            if("service".equals(logType)){

            }else if ("button".equals(logType)){

            }else if ("basic".equals(logType)){

            }
        }
        //rabbitTemplate.setConfirmCallback(confirmCallback);
        //rabbitTemplate.setReturnCallback(returnCallback);
        CorrelationData correlationData = new CorrelationData("0987654321");


        rabbitTemplate.convertAndSend("exchange-2", "springboot.def", "", correlationData);
    }

}